﻿using Inovout.Kuajing.Exports.Models;
using Inovout.Kuajing.Exports.Services;
using Inovout.Services;
using Inovout.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Kuajing.Exports.Console.Web.Controllers
{
    public class CommodityController : KJBaseController<ExportCommodity>
    {
        private ICommodityService commodityService;
        public CommodityController(ICommodityService commodityService)
        {
            this.commodityService = commodityService;

        }

        [HttpPost]
        public JsonResult GetAllCommidity(int exporterId)
        {
            IRepositoryService<Exporter> exportService = ComponentRegistry.Resolve<IRepositoryService<Exporter>>();
            Exporter exporter = exportService.FindById(exporterId);
            IList<ExportCommodity> listCommodity = commodityService.FindCommodityByExporter(exporter);

            var data = from c in listCommodity
                       select new { Id = c.Id, BM = c.Id, PM = c.Product.Name, XH = c.Product.Model, PL = c.Product.Brand };
            return Json(data, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult GetAllCommidities(int orderId)
        {
            IRepositoryService<TraditionalOrder> traditionalOrderService = ComponentRegistry.Resolve<IRepositoryService<TraditionalOrder>>();
            TraditionalOrder traditionalOrder = traditionalOrderService.FindById(orderId);

            IRepositoryService<Exporter> exportService = ComponentRegistry.Resolve<IRepositoryService<Exporter>>();
            Exporter exporter = exportService.FindById(traditionalOrder.Exporter.Id);
            IList<ExportCommodity> listCommodity = commodityService.FindCommodityByExporter(exporter);

            List<Object> data = new List<Object>();

            if (traditionalOrder.Items != null && traditionalOrder.Items.Count() > 0)
            {
                foreach (TraditionalOrderItem traditionalOrderItem in traditionalOrder.Items)
                {
                    data.Add(new
                    {
                        Id = traditionalOrderItem.Commodity.Id,
                        BM = traditionalOrderItem.Commodity.Id,
                        PM = traditionalOrderItem.Commodity.Product.Name,
                        XH = traditionalOrderItem.Commodity.Product.Model,
                        PL = traditionalOrderItem.Commodity.Product.Brand,
                        Brawer = traditionalOrderItem.Drawer == null ? "" : traditionalOrderItem.Drawer.Name,
                        BrawerCode = traditionalOrderItem.Drawer == null ? 0 : traditionalOrderItem.Drawer.Id,
                        orderItemId = traditionalOrderItem.Id
                    });
                    listCommodity.Remove(traditionalOrderItem.Commodity);
                }
            }

            foreach (ExportCommodity commodity in listCommodity)
            {
                data.Add(new
                {
                    Id = commodity.Id,
                    BM = commodity.Id,
                    PM = commodity.Product.Name,
                    XH = commodity.Product.Model,
                    PL = commodity.Product.Brand,
                    Brawer = "",
                    BrawerCode = 0,
                    orderItemId = 0
                });
            }

            //data = from c in listCommodity
            //          select new { Id = c.Id, BM = c.Id, PM = c.Product.Name, XH = c.Product.Model, PL = c.Product.Brand };
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}